package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.entity.SysUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo. MailboxAppUserBo;
import com.ruoyi.wms.domain.vo. MailboxAppUserVo;
import com.ruoyi.wms.domain.entity. MailboxAppUser;
import com.ruoyi.wms.mapper. MailboxAppUserMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户管理Service业务层处理
 *
 * @author zcc
 * @date 2024-12-16
 */
@RequiredArgsConstructor
@Service
public class  MailboxAppUserService {

    private final  MailboxAppUserMapper  MailboxAppUserMapper;

    /**
     * 查询用户管理
     */
    public  MailboxAppUserVo queryById(Long userId){
        return  MailboxAppUserMapper.selectVoById(userId);
    }

    /**
     * 查询用户管理列表
     */
    public TableDataInfo< MailboxAppUserVo> queryPageList( MailboxAppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper< MailboxAppUser> lqw = buildQueryWrapper(bo);
        Page< MailboxAppUserVo> result =  MailboxAppUserMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    public int updateUserStatus(Long userId, String status) {
        return MailboxAppUserMapper.update(null,
            new LambdaUpdateWrapper<MailboxAppUser>()
                .set(MailboxAppUser::getStatus, status)
                .eq(MailboxAppUser::getUserId, userId));
    }

    /**
     * 查询用户管理列表
     */
    public List< MailboxAppUserVo> queryList( MailboxAppUserBo bo) {
        LambdaQueryWrapper< MailboxAppUser> lqw = buildQueryWrapper(bo);
        return  MailboxAppUserMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper< MailboxAppUser> buildQueryWrapper( MailboxAppUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper< MailboxAppUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()),  MailboxAppUser::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()),  MailboxAppUser::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()),  MailboxAppUser::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()),  MailboxAppUser::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()),  MailboxAppUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()),  MailboxAppUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()),  MailboxAppUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getSalt()),  MailboxAppUser::getSalt, bo.getSalt());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()),  MailboxAppUser::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()),  MailboxAppUser::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null,  MailboxAppUser::getLoginDate, bo.getLoginDate());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()),  MailboxAppUser::getOpenid, bo.getOpenid());
        lqw.orderByDesc(MailboxAppUser::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户管理
     */
    public void insertByBo( MailboxAppUserBo bo) {
         MailboxAppUser add = MapstructUtils.convert(bo,  MailboxAppUser.class);
         MailboxAppUserMapper.insert(add);
    }

    /**
     * 修改用户管理
     */
    public void updateByBo( MailboxAppUserBo bo) {
         MailboxAppUser update = MapstructUtils.convert(bo,  MailboxAppUser.class);
         MailboxAppUserMapper.updateById(update);
    }

    /**
     * 批量删除用户管理
     */
    public void deleteByIds(Collection<Long> ids) {
         MailboxAppUserMapper.deleteBatchIds(ids);
    }
}
